package cn.com.sbell.vss.commander;

import javax.servlet.http.HttpServletRequest;

import cn.com.sbell.vss.ICommander;
import cn.com.sbell.vss.Response;
import cn.com.sbell.vss.Util;
import cn.com.sbell.vss.VssConstants;
import cn.com.sbell.vss.monitor.PerformanceMonitor;

/**
 * @author Yifu.HUANG
 * 
 */
public class SetPerformanceCheckInterval implements ICommander {
	private PerformanceMonitor maPerformanceMonitor;

	private PerformanceMonitor csgPerformanceMonitor;

	private PerformanceMonitor emsPerformanceMonitor;

	/**
	 * (non-Javadoc)
	 * 
	 * @see cn.com.sbell.vss.ICommander#execute(javax.servlet.http.HttpServletRequest)
	 */
	public Response execute(HttpServletRequest request) {
		int i = Util.getIntParameter(request, "interval", true, "interval");
		long interval = 1000L * i;
		String serverType = Util.getStringParameter(request, "serverType",
				true, 32, "serverType");
		if (VssConstants.SOURCE_TYPE_EMS.equals(serverType)
				&& emsPerformanceMonitor != null) {
			emsPerformanceMonitor.setInterval(interval);
			if(emsPerformanceMonitor.getPerformanceMonitorThread()!=null){
				emsPerformanceMonitor.getPerformanceMonitorThread().interrupt();
			}
		} else if (VssConstants.SOURCE_TYPE_CSG.equals(serverType)
				&& csgPerformanceMonitor != null) {
			csgPerformanceMonitor.setInterval(interval);
			if(csgPerformanceMonitor.getPerformanceMonitorThread()!=null){
				csgPerformanceMonitor.getPerformanceMonitorThread().interrupt();
			}
		} else if (VssConstants.SOURCE_TYPE_MA.equals(serverType)
				&& maPerformanceMonitor != null) {
			maPerformanceMonitor.setInterval(interval);
			if(maPerformanceMonitor.getPerformanceMonitorThread()!=null){
				maPerformanceMonitor.getPerformanceMonitorThread().interrupt();
			}
		}
		Response response = Response.createResponse(request);
		response.setReturnCode(VssConstants.CODE_SUCCESS);
		return response;
	}

	public void setCsgPerformanceMonitor(
			PerformanceMonitor csgPerformanceMonitor) {
		this.csgPerformanceMonitor = csgPerformanceMonitor;
	}

	public void setEmsPerformanceMonitor(
			PerformanceMonitor emsPerformanceMonitor) {
		this.emsPerformanceMonitor = emsPerformanceMonitor;
	}

	public void setMaPerformanceMonitor(PerformanceMonitor maPerformanceMonitor) {
		this.maPerformanceMonitor = maPerformanceMonitor;
	}

}
